<template>
  <div class="app-container">
    <el-form :model="queryParam" ref="queryForm" :inline="true">
      <el-form-item label="工作单位：">
        <el-input v-model="queryParam.workPlace" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">查询</el-button>
        <el-button @click="clearForm">重置</el-button>
        <el-button type="primary" @click="add">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column prop="userRealName" label="姓名" />
      <el-table-column prop="workTime" label="起讫年月" />
      <el-table-column prop="workPlace" label="工作单位" />
      <el-table-column prop="workTitle" label="职务/岗位" />
      <el-table-column prop="workScore" label="取得的主要工作成绩" />
      <el-table-column prop="workCertifier" label="证明人" />
      <el-table-column width="160px" label="操作" align="center">
        <template #default="{ row }">
          <el-button size="small" class="wdd-button-mini" @click="editItem(row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            @click="userDelete(row)"
            size="small"
            class="wdd-button-mini link-left"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      style="margin-top: 15px"
      :current-page="queryParam.pageIndex"
      :page-sizes="[10, 20, 30, 100]"
      :page-size="queryParam.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog
      v-model="showDialog"
      :title="dialogTitle"
      width="800px"
      center="true"
    >
      <el-form :model="form" ref="form" label-width="140px" :rules="rules">
        <el-form-item label="起讫年月:" prop="workTime" required>
          <el-input
            v-model="form.workTime"
            placeholder="请输入起讫年月"
            clearable
          />
        </el-form-item>
        <el-form-item label="工作单位：" prop="workPlace" required>
          <el-input
            v-model="form.workPlace"
            placeholder="请输入工作单位"
            clearable
          />
        </el-form-item>
        <el-form-item label="职务/岗位：" prop="workTitle" required>
          <el-input v-model="form.workTitle" placeholder="请输入职务/岗位" clearable />
        </el-form-item>
        <el-form-item label="取得的主要工作成绩：" prop="workScore" required>
          <el-input v-model="form.workScore" placeholder="请输入取得的主要工作成绩" clearable />
        </el-form-item>
        <el-form-item label="证明人：" prop="workCertifier" required>
          <el-input v-model="form.workCertifier" placeholder="请输入证明人" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: right">
        <el-button @click="showDialog = false">取 消</el-button>
        <el-button type="primary" @click="sureAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  userexperiencePage,
  userexperienceInsert,
  userexperienceDelete,
  userexperienceUpdate
} from '@/api/user';

export default {
  name: 'EmployeeRule',
  data() {
    return {
      queryParam: {
        pageIndex: 1,
        pageSize: 10,
        userId: '',
      },
      tableData: [],
      total: 0,
      canLoad: true,
      showDialog: false,
      form: {
        id: '',
        workPlace: '',
        workTime: '',
        workScore: '',
        workCertifier: '',
        workTitle: '',
        userId: ''
      },
      dialogTitle: '新增'
    };
  },
  mounted() {
    this.queryParam.userId = this.$route.query.userId;
    this.form.userId = this.$route.query.userId;
    this.init();
    this.canLoad = false;
  },
  activated() {
    this.init();
  },
  deactivated() {
    this.canLoad = true;
  },
  methods: {
    init() {
      if (!this.canLoad) {
        return;
      }
      this.search();
    },
    search() {
      userexperiencePage(this.queryParam).then(data => {
        const re = data.response;
        this.tableData = re.list;
        this.total = re.total;
        this.queryParam.pageIndex = re.pageNum;
      });
    },
    userDelete(row) {
      this.$confirm('将执行删除操作, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userexperienceDelete({ id: row.id }).then(re => {
          if (re.code === 1) {
            this.search();
            this.$message.success(re.message);
          } else {
            this.$message.error(re.message);
          }
        });
      });
    },
    editItem(row) {
      for (const i in this.form) {
        this.form[i] = row[i];
      }
      this.showDialog = true;
      this.dialogTitle = '编辑';
    },
    submitForm() {
      this.queryParam.pageIndex = 1;
      this.search();
    },
    clearForm() {
      this.queryParam.pageIndex = 1;
      this.queryParam.violation = '';
      this.search();
    },
    handleSizeChange(val) {
      this.queryParam.pageSize = val;
      this.search();
    },
    handleCurrentChange(val) {
      this.queryParam.pageIndex = val;
      this.search();
    },
    add() {
      this.showDialog = true;
      this.dialogTitle = '新增';
      this.resetForm();
    },
    sureAdd() {
      if (this.form.id) {
        userexperienceUpdate(this.form).then(res => {
          if (res.code === 1) {
            this.$message.success(res.message);
            this.showDialog = false;
            this.search();
          } else {
            this.$message.error(res.message);
          }
        });
        this.resetForm();
      } else {
        userexperienceInsert(this.form).then(res => {
          if (res.code === 1) {
            this.$message.success(res.message);
            this.showDialog = false;
            this.search();
          } else {
            this.$message.error(res.message);
          }
          this.resetForm();
        });
      }
    },
    resetForm() {
      this.form.id = '';
      this.form.violation = '';
      this.form.violationLevel = '';
      this.form.violationResult = '';
      this.form.note = '';
      this.form.violationTime = '';
      this.$refs['form'].resetFields();
    }
  }
};
</script>
